﻿using System;
using System.Web.UI.WebControls;
using System.Data;

public partial class NPS_app_MasterData_MarketingTypeMaster : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrTitle.Text = "Master Data: Marketing Activity Type";
            loadView();
            setPanel();
            pnlView.Visible = true;
        }
        else
        {
            if (pnlView.Visible)
            {
                loadView();
            }
        }
    }


    private void setPanel()
    {
        pnlView.Visible = false;
        pnlCreate.Visible = false;
        pnlEdit.Visible = false;
        pnlDetails.Visible = false;
        pnlDelete.Visible = false;
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void gv_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        loadData(e.CommandArgs.CommandArgument.ToString());
        if (e.CommandArgs.CommandName == "Editing")
        {
            goEdit();
        }
        else if (e.CommandArgs.CommandName == "Details")
        {
            goDetail();
        }
        else if (e.CommandArgs.CommandName == "Deleting")
        {
            goDelete();
        }
    }



    protected void btnCreate_Click(object sender, EventArgs e)
    {
        goNew();
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        loadView();
    }
    protected void btnCSave_Click(object sender, EventArgs e)
    {
        Entities.NPSMarketingType marketingName = new Entities.NPSMarketingType()
        {
            sxCreator = ICPUser.ID,
            sxTitle = txtCTitle.Text,
            sxDetail = txtCDetail.Text
        };
        saveMarketingType(marketingName);
    }
    protected void btnCCancel_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnESave_Click(object sender, EventArgs e)
    {
        Entities.NPSMarketingType marketingName = new Entities.NPSMarketingType()
        {
            iID = getID,
            sxUpdater = ICPUser.ID,
            sxTitle = txtETitle.Text,
            sxDetail = txtEDetail.Text
        };
        updateMarketingType(marketingName);
    }
    protected void btnECancel_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDEdit_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDBack_Click(object sender, EventArgs e)
    {
        goView();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Entities.NPSMarketingType marketingName = new Entities.NPSMarketingType()
        {
            iID = getID,
            sxCreator = ICPUser.ID
        };
        deleteMarketingType(marketingName);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        goView();
    }

    #region Audy's Custom Function

    protected int getID
    {
        get { return Convert.ToInt32(hfID.Value); }
    }
    protected void goView()
    {
        setPanel();
        pnlView.Visible = true;
        ltrTitle.Text = "Master Data Marketing Type";
        txtCDetail.Text = string.Empty;
        txtCTitle.Text = string.Empty;
    }

    protected void goEdit()
    {
        setPanel();
        pnlEdit.Visible = true;
        ltrTitle.Text = "Master Data Marketing Type Edit";
    }

    protected void goNew()
    {
        setPanel();
        pnlCreate.Visible = true;
        ltrTitle.Text = "Master Data Marketing Type Create";
    }

    protected void goDetail()
    {
        setPanel();
        pnlDetails.Visible = true;
        ltrTitle.Text = "Master Data Marketing Type Details";
    }

    protected void goDelete()
    {
        setPanel();
        pnlDelete.Visible = true;
        ltrTitle.Text = "Master Data Marketing Type Delete";
    }

    protected void loadView()
    {
        Controller.NPSMarketingTypeController mnc = new Controller.NPSMarketingTypeController();
        GridView1.DataSource = mnc.NPSMarketingTypeList;
        GridView1.DataBind();
    }
    protected void loadData(string id)
    {
        //fetch data from DB
        Controller.NPSMarketingTypeController ANController = new Controller.NPSMarketingTypeController();
        Entities.NPSMarketingType MarketingType = ANController.getNPSMarketingType(Convert.ToInt32(id));

        //set all data to forms
        txtETitle.Text = MarketingType.sxTitle;
        lblDT.Text = MarketingType.sxTitle;
        lblT.Text = MarketingType.sxTitle;

        txtEDetail.Text = MarketingType.sxDetail;
        lblDD.Text = MarketingType.sxDetail;
        lblD.Text = MarketingType.sxDetail;

        lblID.Text = MarketingType.iID.ToString();
        lblDID.Text = MarketingType.iID.ToString();
        hfID.Value = MarketingType.iID.ToString();
    }
    protected void saveMarketingType(Entities.NPSMarketingType MarketingType)
    {
        MarketingType.sxCreator = ICPUser.ID;
        Controller.NPSMarketingTypeController ANController = new Controller.NPSMarketingTypeController();
        if (ANController.InsertNPSMarketingTypeData(MarketingType) == 0)
        { //return false; 
        }
        else
        { //return true;
            loadView();
        }
        goView();
        AlertNotification("Tipe marketing " + MarketingType.sxTitle + " telah tersimpan");
    }

    protected void updateMarketingType(Entities.NPSMarketingType MarketingType)
    {
        Controller.NPSMarketingTypeController ANController = new Controller.NPSMarketingTypeController();
        ANController.UpdateNPSMarketingTypeData(MarketingType);
        loadView();
        goView();
        AlertNotification("Tipe marketing " + MarketingType.sxTitle + " telah diperbaharui");
    }

    protected void deleteMarketingType(Entities.NPSMarketingType MarketingType)
    {
        Controller.NPSMarketingTypeController ANController = new Controller.NPSMarketingTypeController();
        ANController.DeleteNPSMarketingTypeData(MarketingType);
        loadView();
        goView();
        AlertNotification("Tipe marketing " + MarketingType.sxTitle + " telah dihapus");
    }
    #endregion
}
